package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("CrawlerfilestandardetailRestController")
public class CrawlerfilestandardetailRestController extends CmiEmaController {

	/**
	 * Create a new Crawlerfilestandardetail entity
	 * 
	 */
	@RequestMapping(value = "/newCrawlerfilestandardetail", method = RequestMethod.POST)
	@ResponseBody
	public Object newCrawlerfilestandardetail(@RequestBody Crawlerfilestandardetail crawlerfilestandardetail) {
		crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
		return JSONObject.parse(cmiPsonService.toJsonString(
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId()),
				2));
	}

	@RequestMapping(value = "/restNewCrawlerfilestandardetail", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewCrawlerfilestandardetail(@RequestBody String crawlerfilestandardetailJson) {
		Crawlerfilestandardetail crawlerfilestandardetail = new Gson().fromJson(crawlerfilestandardetailJson,
				Crawlerfilestandardetail.class);
		appendReferences(crawlerfilestandardetail);
		crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
		return JSONObject.parse(cmiPsonService.toJsonString(
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId()),
				2));
	}

	@RequestMapping(value = "/saveCrawlerfilestandardetail", method = RequestMethod.PUT)
	@ResponseBody
	public Crawlerfilestandardetail saveCrawlerfilestandardetail(
			@RequestBody Crawlerfilestandardetail crawlerfilestandardetail) {
		crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
		return crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId());
	}

	@RequestMapping(value = "/restSaveCrawlerfilestandardetail", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveCrawlerfilestandardetail(@RequestBody String crawlerfilestandardetailJson) {
		Crawlerfilestandardetail crawlerfilestandardetail = new Gson().fromJson(crawlerfilestandardetailJson,
				Crawlerfilestandardetail.class);
		appendReferences(crawlerfilestandardetail);
		CmiPsonService cmiPsonService = new CmiPsonService();
		crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
		return JSONObject.parse(cmiPsonService.toJsonString(
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId()),
				1));
	}

	@RequestMapping(value = "/restDeleteCrawlerfilestandardetail/{crawlerfilestandardetailId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteCrawlerfilestandardetail(@PathVariable Integer crawlerfilestandardetailId) {
		Crawlerfilestandardetail crawlerfilestandardetail = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId);
		crawlerfilestandardetailService.deleteCrawlerfilestandardetail(crawlerfilestandardetail);
	}

	@RequestMapping(value = "/restConfirmDeleteCrawlerfilestandardetails")
	@ResponseBody
	public Object restConfirmDeleteCrawlerfilestandardetails(
			@RequestBody Set<Integer> deleteCrawlerfilestandardetailsIds) {
		Set<Crawlerfilestandardetail> deleteCrawlerfilestandardetails = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailsByIds(deleteCrawlerfilestandardetailsIds);
		Set<Crawlerfilestandardetail> referencedCrawlerfilestandardetails = crawlerfilestandardetailService
				.loadReferenceCrawlerfilestandardetails(deleteCrawlerfilestandardetails);
		deleteCrawlerfilestandardetails.removeAll(referencedCrawlerfilestandardetails);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedCrawlerfilestandardetails);
		jsonObject.put("deleteEntitys", deleteCrawlerfilestandardetails);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteCrawlerfilestandardetails")
	@ResponseBody
	public Object restDeleteCrawlerfilestandardetails(
			@RequestBody Set<Crawlerfilestandardetail> deleteCrawlerfilestandardetails) {
		Iterator<Crawlerfilestandardetail> iterator = deleteCrawlerfilestandardetails.iterator();
		while (iterator.hasNext()) {
			Crawlerfilestandardetail currentCrawlerfilestandardetail = iterator.next();
			crawlerfilestandardetailService.deleteCrawlerfilestandardetail(currentCrawlerfilestandardetail);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listCrawlerfilestandardetails");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadCrawlerfilestandardetail/{crawlerfilestandardetailId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardetail(@PathVariable Integer crawlerfilestandardetailId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restLoadCrawlerfilestandardetailCrawlerfilestandard/{crawlerfilestandardetailId}/crawlerfilestandard", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardetailCrawlerfilestandard(@PathVariable Integer crawlerfilestandardetailId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId).getCrawlerfilestandard(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restLoadCrawlerfilestandardetailCrawlerfilestandardcolumn/{crawlerfilestandardetailId}/crawlerfilestandardcolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilestandardetailCrawlerfilestandardcolumn(
			@PathVariable Integer crawlerfilestandardetailId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId).getCrawlerfilestandardcolumn(), 2,
				cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	public void appendReferences(Crawlerfilestandardetail crawlerfilestandardetail) {

		Crawlerfilestandard crawlerfilestandard = crawlerfilestandardetail.getCrawlerfilestandard();
		if (crawlerfilestandard != null)
			crawlerfilestandardetail.setCrawlerfilestandard(
					crawlerfilestandardService.findCrawlerfilestandardByPrimaryKey(crawlerfilestandard.getId()));

		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardetail.getCrawlerfilestandardcolumn();
		if (crawlerfilestandardcolumn != null)
			crawlerfilestandardetail.setCrawlerfilestandardcolumn(crawlerfilestandardcolumnService
					.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId()));

	}

	@RequestMapping(value = "/restGetCrawlerfilestandardetailCrawlerfilestandard/{crawlerfilestandardetailId}/crawlerfilestandard", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetCrawlerfilestandardetailCrawlerfilestandard(@PathVariable Integer crawlerfilestandardetailId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId).getCrawlerfilestandard(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restGetCrawlerfilestandardetailCrawlerfilestandardcolumn/{crawlerfilestandardetailId}/crawlerfilestandardcolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetCrawlerfilestandardetailCrawlerfilestandardcolumn(
			@PathVariable Integer crawlerfilestandardetailId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId).getCrawlerfilestandardcolumn(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restListCrawlerfilestandardetails", method = RequestMethod.GET)
	@ResponseBody
	public Object restListCrawlerfilestandardetails() {
		return JSONObject
				.parse(cmiPsonService.toJsonString(crawlerfilestandardetailService.loadCrawlerfilestandardetails(), 2));
	}

	@RequestMapping(value = "/restListCrawlerfilestandardetailsPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListCrawlerfilestandardetailsPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = crawlerfilestandardetailPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Crawlerfilestandardetail> crawlerfilestandardetails = crawlerfilestandardetailDAO
				.findAllCrawlerfilestandardetails(startResult, cmiPagination.getPaginationSize());
		String crawlerfilestandardetailString = cmiPsonService.toPsonString(crawlerfilestandardetails, 2, cmiPagination);

		return JSONObject.parse(crawlerfilestandardetailString);
	}

	@RequestMapping("/crawlerfilestandardetailSelectBox")
	@ResponseBody
	public JSONObject crawlerfilestandardetailSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键
			case "Crawlerfilestandard":
				entitySet = crawlerfilestandardService.loadCrawlerfilestandardsByIdSet(searchValue);
				break;
			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnService.loadCrawlerfilestandardcolumnsByIdSet(searchValue);
				break;

			// 被引用键
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键
			case "Crawlerfilestandard":
				entitySet = crawlerfilestandardDAO.findAllCrawlerfilestandards((pageNumber - 1) * paginationSize,
						paginationSize);
				count = crawlerfilestandardService.countCrawlerfilestandards();
				break;
			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnDAO
						.findAllCrawlerfilestandardcolumns((pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardcolumnService.countCrawlerfilestandardcolumns();
				break;

			// 被引用键
			}
		} else {
			switch (selectItem) {
			// 外键
			case "Crawlerfilestandard":
				entitySet = crawlerfilestandardDAO.findCrawlerfilestandardByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardService.countCrawlerfilestandardsByName(queryword);
				break;
			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardcolumnService.countCrawlerfilestandardcolumnsByName(queryword);
				break;

			// 被引用键
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
